package com.songxh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.songxh.common.BaseContext;
import com.songxh.common.R;
import com.songxh.dto.OrdersDto;
import com.songxh.pojo.OrderDetail;
import com.songxh.pojo.Orders;
import com.songxh.service.OrderDetailService;
import com.songxh.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author songxh
 * @create 2022-04-27-22:05
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;


    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {

        orderService.submitData(orders);

        return R.success("支付成功");

    }


    /**
     * 查看订单详情
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPager(@RequestParam int page, @RequestParam int pageSize) {
        Page<Orders> detailPage = new Page<>(page, pageSize);
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("order_time");
        orderService.page(detailPage, queryWrapper);
        return R.success(detailPage);
    }


    /**
     * 后台的订单管理
     * @param page
     * @param pageSize
     * @param number
     * @return
     */
    //order/page?page=1&pageSize=10
    @GetMapping("/page")
    public R<Page> page(@RequestParam int page, @RequestParam int pageSize, Long number) {

        //思路是将order的page的数据转到OrdersDto的怕个中
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> dtoPage = new Page<>();

        //查询判断以及排序
        QueryWrapper<Orders> queryWrapper=new QueryWrapper<>();
        if (number!=null){
            queryWrapper.like("id",number);
        }
        queryWrapper.orderByDesc("order_time");
        orderService.page(pageInfo,queryWrapper);

        //将page的数据拷贝，除了records，records存放的是每一条数据
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list=new ArrayList<>();
        for (Orders record : records) {
            //将每一条order数据拷贝，因为OrdersDto继承了order
            OrdersDto dto = new OrdersDto();
            BeanUtils.copyProperties(record,dto);

            //获取orderdetaiks
            QueryWrapper<OrderDetail> detailQueryWrapper=new QueryWrapper<>();
            Long orderId = record.getId();
            detailQueryWrapper.eq("order_id",orderId);
            List<OrderDetail> detailList = orderDetailService.list(detailQueryWrapper);
            dto.setOrderDetails(detailList);

            //获取用户id(数据表中的用户名就是consignee，这是设计数据表的人的问题)
            String consignee = record.getConsignee();
            dto.setUserName(consignee);

            list.add(dto);
        }
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }


    /**
     * 在后台的订单管理当中修改派送信息
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders){
        QueryWrapper<Orders> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",orders.getId());
        Orders orderss = orderService.getOne(queryWrapper);
        orderss.setStatus(orders.getStatus());
        orderService.updateById(orderss);
        return R.success("派送成功");

    }


    /**
     * 再来一单操作
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders){
        QueryWrapper<Orders> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",orders.getId());
        Orders reOrders = orderService.getOne(queryWrapper);

        //生成一个id
        long id = IdWorker.getId();

        //设置第二弹的属性
        reOrders.setId(id);
        reOrders.setStatus(2);
        reOrders.setNumber(String.valueOf(id));
        reOrders.setOrderTime(LocalDateTime.now());
        reOrders.setCheckoutTime(LocalDateTime.now());

        orderService.save(reOrders);

        //设置第二单的订单明细
        QueryWrapper<OrderDetail> wrapper=new QueryWrapper<>();
        wrapper.eq("order_id", orders.getId());
        List<OrderDetail> details = orderDetailService.list(wrapper);
        for (OrderDetail detail : details) {
            long id1 = IdWorker.getId();
            detail.setId(id1);
        }
        orderDetailService.saveBatch(details);

        return R.success("再来一单成功");
    }

}
